/**
 * 
 */
package tk.redbluelotto.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

/**
 * @author zb
 */
public class FetchLastNumber {
	
    private static final Logger log = Logger.getLogger(FetchLastNumber.class.getName());

	private static long[] lastValue;

	private static long lastUpdateTime;
	
	public static long[] getLastValue(int num) {
		if (lastValue == null || (System.currentTimeMillis() - lastUpdateTime) > 86400000) {
			try {
				lastValue = WanHtmlParser.getlastValue(num, 0);
				lastUpdateTime = System.currentTimeMillis();
			} catch (Exception e) {
				log.severe(e.getMessage());
			}
		}

		return lastValue;
	}

	public static void setLastValue(long[] lastValue) {
		FetchLastNumber.lastValue = lastValue;
	}

	public static String loadFromWeb(String urlStr){
		StringBuffer sb = new StringBuffer(10240);
		try {
			URL url = new URL(urlStr);
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
		} catch (MalformedURLException e) {
			log.severe(e.getMessage());
		} catch (IOException e) {
			log.severe(e.getMessage());
		} 
		return sb.toString();
	}

}
